//
//  HomeComicsListViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/12.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class HomeComicsListViewController: UIViewController {

    fileprivate var argCon: Int = 0
    fileprivate var argName: String?
    fileprivate var argValue: Int = 0
    fileprivate var page: Int = 1
    
    //数据源
    fileprivate var comicsTypeListArr = [ComicsTypeModel]()
    fileprivate var spinnerName: String?
    
    fileprivate lazy var tableView: UITableView = {
       let tableView = UITableView(frame: .zero, style: .plain)
        tableView.tableFooterView = UIView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 180
        tableView.register(ComicsTypeListCell.self, forCellReuseIdentifier: "ComicsTypeListCell")
        tableView.refrshHead = RefreshGifHeader{
            self.loadData(more: false)
        }
        tableView.refreshFoot = RefreshBackNormalFooter {
            self.loadData(more: true)
        }
        return tableView
    }()
    
    //便利函数
    convenience init(argCon: Int = 0, argName: String?, argValue: Int = 0) {
        self.init()
        self.argCon = argCon
        self.argName = argName
        self.argValue = argValue
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "nav_bg").withColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        

        ///////
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        loadData(more: false)
        view.addSubview(self.tableView)
        setUpSnap()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //导航栏按钮颜色
        navigationController?.navigationBar.tintColor = UIColor.black
        //导航栏字体颜色以及大小
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black , NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17)]
        UIApplication.shared.statusBarStyle = .default
    }
    
    fileprivate func setUpSnap() {
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.usnp.edges)
        }
    }
    
    fileprivate func loadData(more: Bool) {
        page = (more ? (page + 1): 1)
        NetworkTool.loadComicList(argCon: argCon, argName: argName ?? "", argValue: argValue, page: page) {[weak self] (model) in
            self?.tableView.refrshHead.endRefreshing()
            if model.hasMore == false {
                self?.tableView.refreshFoot.endRefreshingWithNoMoreData()
            }else {
                self?.tableView.refreshFoot.endRefreshing()
            }
            if !more {
                self?.comicsTypeListArr.removeAll()
            }
            for item in model.comics {
                self?.comicsTypeListArr.append(item)
            }
            self?.tableView.reloadData()
            
            guard let defaultP:DefaultParametersModel = model.defaultParameters else {
                return
            }
            self?.argCon = defaultP.defaultArgCon
            self?.spinnerName = defaultP.defaultConTagType
            
        }
    }

}

extension HomeComicsListViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return comicsTypeListArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ComicsTypeListCell", for: indexPath) as! ComicsTypeListCell
        cell.indexPath = indexPath
        //⚠️model里的didSet 以及下面代码顺序 ，会影响到spinnerName是否有值
        cell.spinnerName = spinnerName
        cell.model = comicsTypeListArr[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.deselectRow(at: indexPath, animated: true)
        let model = comicsTypeListArr[indexPath.row]
        let vc = ComicsDetailViewController(comicid: model.comicId)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}






